﻿using System;
using System.Collections.Generic;

namespace _03_ObserverModel_Interface
{
    interface Subject
    {
        String SubjectState { get; set; }
        void Attach(Observer observer);
        void Detach(Observer observer);
        void Notify();
    }
    class Boss : Subject
    {
        public string SubjectState { get ; set; }
        private IList<Observer> observerList = new List<Observer>(); // 观察者同事列表

        /// <summary>
        /// 添加要通知的同事
        /// </summary>
        /// <param name="observer"></param>
        public void Attach(Observer observer)
        {
            observerList.Add(observer);
        }

        public void Detach(Observer observer)
        {
            observerList.Remove(observer);
        }

        /// <summary>
        /// 通知
        /// </summary>
        public void Notify()
        {
            foreach (Observer item in observerList)
            {
                item.Update();
            }
        }
    }
    /// <summary>
    /// 前台秘书类
    /// </summary>
    class Secreatary : Subject
    {
        public string SubjectState { get; set; }   // 通知的事

        private IList<Observer> observerList = new List<Observer>(); // 观察者同事列表

        /// <summary>
        /// 添加要通知的同事
        /// </summary>
        /// <param name="observer"></param>
        public void Attach(Observer observer)
        {
            observerList.Add(observer);
        }
        public void Detach(Observer observer)
        {
            observerList.Remove(observer);
        }
        /// <summary>
        /// 通知
        /// </summary>
        public void Notify()
        {
            foreach (Observer item in observerList)
            {
                item.Update();
            }
        }
    }

    abstract class Observer
    {
        public Observer(string name, Subject sub)
        {
            this.name = name;
            this.sub = sub;
        }
        public string name;  // 名字
        public Subject sub; // 前台秘书
        public abstract void Update();
    }

    /// <summary>
    /// 看股票的观察者
    /// </summary>
    class StockObserver : Observer
    {
        public StockObserver(string name, Subject sub) : base(name, sub)
        {
        }

        public override void Update()
        {
            Console.WriteLine($"{sub.SubjectState},{name}关闭股票行情，继续工作！");
        }
    }

    /// <summary>
    /// 看NBA的观察者
    /// </summary>
    class NBAObserver : Observer
    {
        public NBAObserver(string name, Subject sub) : base(name, sub)
        {
        }

        public override void Update()
        {
            Console.WriteLine($"{sub.SubjectState},{name}关闭股票行情，继续工作！");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Boss boss = new Boss();
            // 看股票的同事
            StockObserver stockObserver1 = new StockObserver("为观察", boss);
            StockObserver stockObserver2 = new StockObserver("一观察", boss);
            // 前台记下要通知的同事
            boss.Attach(stockObserver1);
            boss.Attach(stockObserver2);
            // 发现老板来了
            boss.SubjectState = "老板来了";
            boss.Detach(stockObserver1);
            // 通知
            boss.Notify();

            Console.Read();
        }
    }

}
